package zozopo.background.operator;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;



import zozopo.background.operator.operations.LimitedTB;
import zozopo.background.operator.operations.OperatorOS;
import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedOS;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.comm.Validate;
import zozopo.log.operations.OperatorLoginLogOS;
import zozopo.log.operations.OperatorLoginLogTB;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import com.opensymphony.xwork2.ActionSupport;



import com.opensymphony.xwork2.Action;


public class LoginAction extends ActionSupport implements Action ,SessionAware, ServletRequestAware{
	private static final Log logger = LogFactory.getLog(LogFactory.class);

	private HttpServletRequest request;
	private Map<Object, Object> session;
	
	private String redirect;
	
	private String email;
	private String operatorname;
	private String password;
	private String valicode;
	
	

	
	public String execute() throws Exception {
		//ActionContext actionContext = ActionContext.getContext(); 
		
		
		return "success";
	}
	
	public void validate() {
		
		
		//验证码
		boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(request, valicode);
		
		
		OperatorLoginLogOS operatorLoginLogOS = new OperatorLoginLogOS();
		OperatorOS operatorOS = new OperatorOS(operatorLoginLogOS.getConn());
		
		
		try { 
		
			OperatorLoginLogTB operatorLoginLog = new OperatorLoginLogTB();
			operatorLoginLog.setType("界面");
			operatorLoginLog.setAccount(email);
			operatorLoginLog.setIp(request.getRemoteAddr());
			
			if(!operatorLoginLogOS.isAccountSecurity(email)){// 如果超出输入密码错误次数.账号存在异常.
				addFieldError("errormessage","账号异常,存在安全问题.请稍后再输入");
				return;
			}
	
			if(operatorLoginLogOS.isWorngOutOfTime(email)){// 如果超出输入密码错误次数.需要校验证码.
				if(!captchaPassed){
					addFieldError("errormessage",getText("background.login.valicode.iswrong"));
					return;
				}
			}
			
			if(getEmail().trim().equals("")){
				addFieldError("errormessage", getText("background.login.email") + getText("background.common.isnotnull"));
				return;
			}
			
			if(email.length() > 320){
				addFieldError("errormessage","email大于320个字符,请重新输入");
				return;
			}
			
			if(! Validate.isEmail(email)){
				addFieldError("errormessage","e-mail格式错误!请输入正确的e-mail.");
				return;
			}
			
			
			
			if(!operatorOS.isRealOperatorByEmail(getEmail(), getPassword())){
				
				//写入错误的登陆日志
				operatorLoginLog.setIswrong(true);
				operatorLoginLogOS.insert(operatorLoginLog);
				
				
				//清空session valicode
				session.put("valicode", "");
				addFieldError("errormessage", getText("background.login.sign.wrong"));
				
				return;
			}
			
			OperatorTB operator = operatorOS.getInfoByEmail(email);
			OperatorLoginLogTB operatorLastLoginLog = operatorLoginLogOS.getLastInfo(email);
			
			
			OperatorlimitedOS userlimitedOS = new OperatorlimitedOS(operatorOS.getConn());
			
			
			Map<String,OperatorlimitedTB> operatorlimitedMap = userlimitedOS.getUserlimitedMap(operator);
			Map<String,List<LimitedTB>> operatorlimitedTree = userlimitedOS.getLimitedTree(operator);
			
			session.put("operatorLastLoginLog", operatorLastLoginLog);
			session.put("operatorInfo", operator);
			session.put("operatorlimitedMap", operatorlimitedMap);
			session.put("operatorlimitedTree", operatorlimitedTree);
			
			//写入登陆日志
			operatorLoginLog.setIswrong(false);
			operatorLoginLogOS.insert(operatorLoginLog);
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			addFieldError("errormessage", "系统错误请联系管理人员");
			return;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			addFieldError("errormessage", "系统错误请联系管理人员");
			return;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			addFieldError("errormessage", "系统错误请联系管理人员");
			return;
		}finally{
			operatorLoginLogOS.releaseConnection();
			//operatorOS.releaseConnection();
		}
		
		
	}
	
	

	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	

	public String getValicode() {
		return valicode;
	}

	public void setValicode(String valicode) {
		this.valicode = valicode;
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	public String getRedirect() {
		return redirect;
	}

	public void setRedirect(String redirect) {
		
		this.redirect = redirect;
	}

	public String getOperatorname() {
		return operatorname;
	}

	public void setOperatorname(String operatorname) {
		this.operatorname = operatorname;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}

	
}

